package com.shrct.businessprocess.project;

import com.google.common.collect.Iterables;
import com.shrct.businessprocess.budget.IHasACTEffectBudget;
import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.budget.RCCost4ACTItem;
import com.shrct.businessprocess.budget.RCCost4RemainItem;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.project.RCActualCostReport;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectTypeExtension;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseDataType;
import java.util.Collection;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

public class RCProjectSummaryInfo extends BaseDataType {
  public RCProjectSummaryInfo() {
    super();
  }
  
  private Double contractAmount;
  
  public Double getContractAmount() {
    return this.contractAmount;
  }
  
  public RCProjectSummaryInfo setContractAmount(final Double contractAmount) {
    this.contractAmount = contractAmount;
    return this;			
    
  }
  
  private Double invoiceAmount;
  
  public Double getInvoiceAmount() {
    return this.invoiceAmount;
  }
  
  public RCProjectSummaryInfo setInvoiceAmount(final Double invoiceAmount) {
    this.invoiceAmount = invoiceAmount;
    return this;			
    
  }
  
  private Double incomeAmount;
  
  public Double getIncomeAmount() {
    return this.incomeAmount;
  }
  
  public RCProjectSummaryInfo setIncomeAmount(final Double incomeAmount) {
    this.incomeAmount = incomeAmount;
    return this;			
    
  }
  
  private Double acceptAmount;
  
  public Double getAcceptAmount() {
    return this.acceptAmount;
  }
  
  public RCProjectSummaryInfo setAcceptAmount(final Double acceptAmount) {
    this.acceptAmount = acceptAmount;
    return this;			
    
  }
  
  public Double getInvoicePrecent() {
    double _xblockexpression = (double) 0;
    {
      Double _nullsafe = NumberExtensions.nullsafe(this.contractAmount);
      boolean _equals = ((_nullsafe).doubleValue() == 0);
      if (_equals) {
        return Double.valueOf(0d);
      }
      Double _nullsafe_1 = NumberExtensions.nullsafe(this.invoiceAmount);
      Double _nullsafe_2 = NumberExtensions.nullsafe(this.contractAmount);
      double _divide = DoubleExtensions.operator_divide(_nullsafe_1, _nullsafe_2);
      _xblockexpression = (_divide);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public Double getIncomePrecent(final RCProject project_t) {
    RCProjectTypeExtension _typeExtension = project_t.getTypeExtension();
    Double _calcPercent = _typeExtension.calcPercent();
    return _calcPercent;
  }
  
  private Double invoiceTax;
  
  public Double getInvoiceTax() {
    return this.invoiceTax;
  }
  
  public RCProjectSummaryInfo setInvoiceTax(final Double invoiceTax) {
    this.invoiceTax = invoiceTax;
    return this;			
    
  }
  
  private Double actualCost;
  
  public Double getActualCost() {
    return this.actualCost;
  }
  
  public RCProjectSummaryInfo setActualCost(final Double actualCost) {
    this.actualCost = actualCost;
    return this;			
    
  }
  
  private Double purchaseTax;
  
  public Double getPurchaseTax() {
    return this.purchaseTax;
  }
  
  public RCProjectSummaryInfo setPurchaseTax(final Double purchaseTax) {
    this.purchaseTax = purchaseTax;
    return this;			
    
  }
  
  public Double getTotalTax(final RCProject project_t) {
    double _xblockexpression = (double) 0;
    {
      Double _nullsafe = NumberExtensions.nullsafe(this.invoiceTax);
      Double _nullsafe_1 = NumberExtensions.nullsafe(this.purchaseTax);
      RCProjectTypeExtension _typeExtension = project_t.getTypeExtension();
      Double _calcPercent = _typeExtension.calcPercent();
      double _multiply = DoubleExtensions.operator_multiply(_nullsafe_1, _calcPercent);
      final double result = ((_nullsafe).doubleValue() - _multiply);
      double _xifexpression = (double) 0;
      boolean _lessThan = (result < 0);
      if (_lessThan) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public Double getAuxTax(final RCProject project_t) {
    double _xblockexpression = (double) 0;
    {
      Double _totalTax = this.getTotalTax(project_t);
      final double result = ((_totalTax).doubleValue() * 0.11);
      double _xifexpression = (double) 0;
      boolean _lessThan = (result < 0);
      if (_lessThan) {
        _xifexpression = 0;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public Double getGrossProfit() {
    Double _nullsafe = NumberExtensions.nullsafe(this.incomeAmount);
    Double _nullsafe_1 = NumberExtensions.nullsafe(this.actualCost);
    double _minus = DoubleExtensions.operator_minus(_nullsafe, _nullsafe_1);
    return Double.valueOf(_minus);
  }
  
  private Double totalCost;
  
  public Double getTotalCost() {
    return this.totalCost;
  }
  
  public RCProjectSummaryInfo setTotalCost(final Double totalCost) {
    this.totalCost = totalCost;
    return this;			
    
  }
  
  public Double calcTotalCost(final RCProject project) {
    double _xblockexpression = (double) 0;
    {
      Double _totalIncomeTaxAmount = project.getTotalIncomeTaxAmount();
      final double taxPart = DoubleExtensions.operator_minus(_totalIncomeTaxAmount, this.purchaseTax);
      double _xifexpression = (double) 0;
      boolean _lessThan = (taxPart < 0);
      if (_lessThan) {
        _xifexpression = 0;
      } else {
        double _multiply = (taxPart * 1.11);
        _xifexpression = _multiply;
      }
      double _plus = ((this.totalCostWithoutTax).doubleValue() + _xifexpression);
      _xblockexpression = (_plus);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  private Double totalCostWithoutTax;
  
  public Double getTotalCostWithoutTax() {
    return this.totalCostWithoutTax;
  }
  
  public RCProjectSummaryInfo setTotalCostWithoutTax(final Double totalCostWithoutTax) {
    this.totalCostWithoutTax = totalCostWithoutTax;
    return this;			
    
  }
  
  public Double calcTotalCostWithoutTax(final RCProject project) {
    double _xblockexpression = (double) 0;
    {
      RCCostBreakDown _cbd = project.getCbd();
      Collection<RCBudget> _budgets = _cbd.getBudgets();
      Iterable<IHasACTEffectBudget> _filter = Iterables.<IHasACTEffectBudget>filter(_budgets, IHasACTEffectBudget.class);
      final Function1<IHasACTEffectBudget,Iterable<RCCost4ACTItem>> _function = new Function1<IHasACTEffectBudget,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final IHasACTEffectBudget it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map = IterableExtensions.<IHasACTEffectBudget, Iterable<RCCost4ACTItem>>map(_filter, _function);
      Iterable<RCCost4ACTItem> _flatten = Iterables.<RCCost4ACTItem>concat(_map);
      final Function1<RCCost4ACTItem,Double> _function_1 = new Function1<RCCost4ACTItem,Double>() {
          public Double apply(final RCCost4ACTItem it) {
            Double _amount = it.getAmount();
            return _amount;
          }
        };
      Iterable<Double> _map_1 = IterableExtensions.<RCCost4ACTItem, Double>map(_flatten, _function_1);
      final Function2<Double,Double,Double> _function_2 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map_1, _function_2);
      final Double cost = NumberExtensions.nullsafe(_reduce);
      RCCostBreakDown _cbd_1 = project.getCbd();
      Collection<RCBudget> _budgets_1 = _cbd_1.getBudgets();
      final Function1<RCBudget,Iterable<RCCost4RemainItem>> _function_3 = new Function1<RCBudget,Iterable<RCCost4RemainItem>>() {
          public Iterable<RCCost4RemainItem> apply(final RCBudget it) {
            Iterable<RCCost4RemainItem> _rCCost4RemainItems = it.getRCCost4RemainItems();
            return _rCCost4RemainItems;
          }
        };
      Iterable<Iterable<RCCost4RemainItem>> _map_2 = IterableExtensions.<RCBudget, Iterable<RCCost4RemainItem>>map(_budgets_1, _function_3);
      Iterable<RCCost4RemainItem> _flatten_1 = Iterables.<RCCost4RemainItem>concat(_map_2);
      final Function1<RCCost4RemainItem,Double> _function_4 = new Function1<RCCost4RemainItem,Double>() {
          public Double apply(final RCCost4RemainItem it) {
            Double _amount = it.getAmount();
            return _amount;
          }
        };
      Iterable<Double> _map_3 = IterableExtensions.<RCCost4RemainItem, Double>map(_flatten_1, _function_4);
      final Function2<Double,Double,Double> _function_5 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce_1 = IterableExtensions.<Double>reduce(_map_3, _function_5);
      final Double remain = NumberExtensions.nullsafe(_reduce_1);
      double _plus = DoubleExtensions.operator_plus(cost, remain);
      _xblockexpression = (_plus);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  public Double calcPurchaseTax(final RCProject project) {
    double _xblockexpression = (double) 0;
    {
      RCCostBreakDown _cbd = project.getCbd();
      Collection<RCBudget> _budgets = _cbd.getBudgets();
      Iterable<IHasACTEffectBudget> _filter = Iterables.<IHasACTEffectBudget>filter(_budgets, IHasACTEffectBudget.class);
      final Function1<IHasACTEffectBudget,Iterable<RCCost4ACTItem>> _function = new Function1<IHasACTEffectBudget,Iterable<RCCost4ACTItem>>() {
          public Iterable<RCCost4ACTItem> apply(final IHasACTEffectBudget it) {
            Iterable<RCCost4ACTItem> _cost4ACItems = it.getCost4ACItems();
            return _cost4ACItems;
          }
        };
      Iterable<Iterable<RCCost4ACTItem>> _map = IterableExtensions.<IHasACTEffectBudget, Iterable<RCCost4ACTItem>>map(_filter, _function);
      final Iterable<RCCost4ACTItem> costItems = Iterables.<RCCost4ACTItem>concat(_map);
      RCCostBreakDown _cbd_1 = project.getCbd();
      Collection<RCBudget> _budgets_1 = _cbd_1.getBudgets();
      final Function1<RCBudget,Iterable<RCCost4RemainItem>> _function_1 = new Function1<RCBudget,Iterable<RCCost4RemainItem>>() {
          public Iterable<RCCost4RemainItem> apply(final RCBudget it) {
            Iterable<RCCost4RemainItem> _rCCost4RemainItems = it.getRCCost4RemainItems();
            return _rCCost4RemainItems;
          }
        };
      Iterable<Iterable<RCCost4RemainItem>> _map_1 = IterableExtensions.<RCBudget, Iterable<RCCost4RemainItem>>map(_budgets_1, _function_1);
      final Iterable<RCCost4RemainItem> remainItems = Iterables.<RCCost4RemainItem>concat(_map_1);
      final Function1<RCCost4ACTItem,Double> _function_2 = new Function1<RCCost4ACTItem,Double>() {
          public Double apply(final RCCost4ACTItem it) {
            Double _tax = it.getTax();
            return _tax;
          }
        };
      Iterable<Double> _map_2 = IterableExtensions.<RCCost4ACTItem, Double>map(costItems, _function_2);
      final Function2<Double,Double,Double> _function_3 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map_2, _function_3);
      Double _nullsafe = NumberExtensions.nullsafe(_reduce);
      final Function1<RCCost4RemainItem,Boolean> _function_4 = new Function1<RCCost4RemainItem,Boolean>() {
          public Boolean apply(final RCCost4RemainItem it) {
            Double _amount = it.getAmount();
            boolean _greaterThan = ((_amount).doubleValue() > 0);
            return Boolean.valueOf(_greaterThan);
          }
        };
      Iterable<RCCost4RemainItem> _filter_1 = IterableExtensions.<RCCost4RemainItem>filter(remainItems, _function_4);
      final Function1<RCCost4RemainItem,Double> _function_5 = new Function1<RCCost4RemainItem,Double>() {
          public Double apply(final RCCost4RemainItem it) {
            Double _tax = it.getTax();
            return _tax;
          }
        };
      Iterable<Double> _map_3 = IterableExtensions.<RCCost4RemainItem, Double>map(_filter_1, _function_5);
      final Function2<Double,Double,Double> _function_6 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce_1 = IterableExtensions.<Double>reduce(_map_3, _function_6);
      Double _nullsafe_1 = NumberExtensions.nullsafe(_reduce_1);
      final double amount = DoubleExtensions.operator_plus(_nullsafe, _nullsafe_1);
      _xblockexpression = (amount);
    }
    return Double.valueOf(_xblockexpression);
  }
  
  private Double contractProfit;
  
  public Double getContractProfit() {
    return this.contractProfit;
  }
  
  public RCProjectSummaryInfo setContractProfit(final Double contractProfit) {
    this.contractProfit = contractProfit;
    return this;			
    
  }
  
  public Double calcContractProfit() {
    double _minus = DoubleExtensions.operator_minus(
      this.contractAmount, this.totalCost);
    return Double.valueOf(_minus);
  }
  
  private Double contractProfitAdj4Sale;
  
  public Double getContractProfitAdj4Sale() {
    return this.contractProfitAdj4Sale;
  }
  
  public RCProjectSummaryInfo setContractProfitAdj4Sale(final Double contractProfitAdj4Sale) {
    this.contractProfitAdj4Sale = contractProfitAdj4Sale;
    return this;			
    
  }
  
  public Double calcContractProfitAdj4Sale(final RCProject project) {
    RCCostBreakDown _cbd = project.getCbd();
    RCProjectMoney _salesActualExCost = _cbd.getSalesActualExCost();
    Double _hostAmount = _salesActualExCost.toHostAmount();
    Double _nullsafe = NumberExtensions.nullsafe(_hostAmount);
    double _plus = DoubleExtensions.operator_plus(
      this.contractProfit, _nullsafe);
    return Double.valueOf(_plus);
  }
  
  private Double ggp;
  
  public Double getGgp() {
    return this.ggp;
  }
  
  public RCProjectSummaryInfo setGgp(final Double ggp) {
    this.ggp = ggp;
    return this;			
    
  }
  
  public Double calcGGP() {
    Double _xblockexpression = null;
    {
      double _minus = DoubleExtensions.operator_minus(this.contractAmount, this.acceptAmount);
      final double result = ((this.contractProfit).doubleValue() - _minus);
      Double _xifexpression = null;
      boolean _and = false;
      boolean _lessThan = (result < 0);
      if (!_lessThan) {
        _and = false;
      } else {
        boolean _lessThan_1 = ((this.contractProfit).doubleValue() < 0);
        _and = (_lessThan && _lessThan_1);
      }
      if (_and) {
        _xifexpression = this.contractProfit;
      } else {
        double _xifexpression_1 = (double) 0;
        boolean _and_1 = false;
        boolean _lessThan_2 = (result < 0);
        if (!_lessThan_2) {
          _and_1 = false;
        } else {
          boolean _greaterThan = ((this.contractProfit).doubleValue() > 0);
          _and_1 = (_lessThan_2 && _greaterThan);
        }
        if (_and_1) {
          _xifexpression_1 = 0d;
        } else {
          _xifexpression_1 = result;
        }
        _xifexpression = _xifexpression_1;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  private Double cggp;
  
  public Double getCggp() {
    return this.cggp;
  }
  
  public RCProjectSummaryInfo setCggp(final Double cggp) {
    this.cggp = cggp;
    return this;			
    
  }
  
  public Double calcCGGP() {
    Double _xblockexpression = null;
    {
      double _minus = DoubleExtensions.operator_minus(this.contractAmount, this.acceptAmount);
      final double result = ((this.contractProfitAdj4Sale).doubleValue() - _minus);
      Double _xifexpression = null;
      boolean _and = false;
      boolean _lessThan = (result < 0);
      if (!_lessThan) {
        _and = false;
      } else {
        boolean _lessThan_1 = ((this.contractProfit).doubleValue() < 0);
        _and = (_lessThan && _lessThan_1);
      }
      if (_and) {
        _xifexpression = this.contractProfitAdj4Sale;
      } else {
        double _xifexpression_1 = (double) 0;
        boolean _and_1 = false;
        boolean _lessThan_2 = (result < 0);
        if (!_lessThan_2) {
          _and_1 = false;
        } else {
          boolean _greaterThan = ((this.contractProfit).doubleValue() > 0);
          _and_1 = (_lessThan_2 && _greaterThan);
        }
        if (_and_1) {
          _xifexpression_1 = 0d;
        } else {
          _xifexpression_1 = result;
        }
        _xifexpression = _xifexpression_1;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  public void performUpdate(final RCProject project, final RCActualCostReport actReport) {
    RCProjectMoney _totalContractAmount = project.getTotalContractAmount();
    Double _hostAmount = _totalContractAmount.toHostAmount();
    this.contractAmount = _hostAmount;
    RCProjectMoney _totalInvoiceAmount = project.getTotalInvoiceAmount();
    Double _hostAmount_1 = _totalInvoiceAmount.toHostAmount();
    this.invoiceAmount = _hostAmount_1;
    Double _totalAcceptedAmount = project.getTotalAcceptedAmount();
    this.acceptAmount = _totalAcceptedAmount;
    RCProjectTypeExtension _typeExtension = project.getTypeExtension();
    Double _calcTotalIncome = _typeExtension.calcTotalIncome();
    this.incomeAmount = _calcTotalIncome;
    Double _totalInvoiceTaxAmount = project.getTotalInvoiceTaxAmount();
    this.invoiceTax = _totalInvoiceTaxAmount;
    Double _calcPurchaseTax = this.calcPurchaseTax(project);
    this.purchaseTax = _calcPurchaseTax;
    Double _calcTotalCostWithoutTax = this.calcTotalCostWithoutTax(project);
    this.totalCostWithoutTax = _calcTotalCostWithoutTax;
    Double _calcTotalCost = this.calcTotalCost(project);
    this.totalCost = _calcTotalCost;
    Double _incomePrecent = this.getIncomePrecent(project);
    double _multiply = DoubleExtensions.operator_multiply(this.totalCost, _incomePrecent);
    this.actualCost = Double.valueOf(_multiply);
    Double _calcContractProfit = this.calcContractProfit();
    this.contractProfit = _calcContractProfit;
    Double _calcContractProfitAdj4Sale = this.calcContractProfitAdj4Sale(project);
    this.contractProfitAdj4Sale = _calcContractProfitAdj4Sale;
    Double _calcGGP = this.calcGGP();
    this.ggp = _calcGGP;
    Double _calcCGGP = this.calcCGGP();
    this.cggp = _calcCGGP;
  }
  
  public static RCProjectSummaryInfo create() {
    return new RCProjectSummaryInfo();
    
    
  }
}
